﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jw.Web.Areas.Pacm.Models;
using System.Web.Http;
using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Mms.Common;
using Newtonsoft.Json.Linq;
using Jw.Web.Areas.Pacm.Common;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Pacm.Controllers
{
    public class DjcglController : BaseController
    {
        //
        // GET: /Pacm/Djcgl/

        public ActionResult Index()
        {
            //年份
            List<ComItem> comYearItems = new List<ComItem>();
            comYearItems.Add(new ComItem() { text = DateTime.Now.AddYears(-1).Year.ToString(), value = DateTime.Now.AddYears(-1).Year.ToString() });
            comYearItems.Add(new ComItem() { text = DateTime.Now.Year.ToString(), value = DateTime.Now.Year.ToString() });
            comYearItems.Add(new ComItem() { text = DateTime.Now.AddYears(1).Year.ToString(), value = DateTime.Now.AddYears(1).Year.ToString() });


            //月份
            List<ComItem> comMonthItems = new List<ComItem>();
            for (int i = 0; i < 12; i++)
            {
                ComItem obj = new ComItem();
                obj.text = obj.value = (i + 1).ToString("00");
                comMonthItems.Add(obj);
            }

            var model = new
            {
                dataSource = new
                {
                    yearsItems = comYearItems,
                    monthItems = comMonthItems,
                    curYear = DateTime.Now.Year.ToString(),
                    curMonth = DateTime.Now.Month.ToString("00")
                },
                form = new
                {
                    Djcgll_Year = DateTime.Now.Date.Year,
                    Djcgll_Month = "",
                    Djcgll_UpdateDate = ""
                }

            };
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            return View();
        }

    }

    public class DjcglApiController : ApiController
    {
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Djcgll_BillNo'>
                <select>
                   p_Djcgll.*
                </select>
                <from>
                    p_Djcgll
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                    <field name='Djcgll_BillNo'             cp='equal'     ></field>
                </where>
            </settings>
            ");
            var pQuery = query.ToParamQuery();
            var list = new p_DjcgllService().GetDynamicListWithPaging(pQuery);
            return list;

        }
        public string GetNewBillNo()
        {
            return new p_DjcgllService().GetNewKey("Djcgll_BillNo", "dateplus");
        }
        public dynamic GetEditMaster(string id)
        {
            p_Djcgll obj = new p_DjcgllService().GetModel(ParamQuery.Instance().AndWhere("Djcgll_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new p_DjcgllService().ScrollKeys("Djcgll_BillNo", id.ToString())
            };
        }
        public void Edit(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Djcgll_BillNo' cp='equal'></field></where>
            </settings>", typeof(p_Djcgll).Name);

            var result = new p_DjcgllService().Edit(null, listWrapper, data);
        }

        public void Delete(string id)
        {
            var result = new p_DjcgllService().Delete(ParamDelete.Instance().AndWhere("Djcgll_BillNo", id));
            MmsHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Djcgll_BillNo={0}]，请重试或联系管理员！", id);
        }

        //审核
        [System.Web.Http.HttpPost]
        public void Audit(string id, JObject data)
        {
            var status = data["status"].ToString();
            var comment = data["comment"].ToString();
            var result = new PacmService().Audit(typeof(p_Djcgll).Name, "Djcgll_BillNo", id, status, comment);
            MmsHelper.ThrowHttpExceptionWhen(result < 0, "单据审核失败[Djcgll_BillNo={0}]，请重试或联系管理员！", id);
        }
    }
}
